<?php

namespace app\kefu\controller;
use think\Controller;
class Webchat extends Controller
{
  
    public function index()
    {
       $user_id=I('get.user_id');
       $p=(int)I('get.p');
       $user=M('users')->field('head_pic,nickname,user_id')->where('user_id',$user_id)->find();
       $pcount=6;
       $total_count=M('service_log')->where('from_uid='.$user_id.' or to_uid='.$user_id)->count();
       $start=($p-1)*$pcount;
       $page=ceil($total_count/$pcount);

       // 查询客服信息
       $kefuinfo=M('service')->select();

       foreach ($kefuinfo as $key => $value) {
         $kefu_info[$value['kf_no']]=$value;
       }
      
       if(IS_AJAX){
          $type=I('get.type');
          if($type=="get_level_mesg"){
            //查询留言信息
            $leve_mesg=M('service_log')->where(['from_uid'=>$user_id,'status'=>'no_kefu_noline'])->select();
            echo json_encode($leve_mesg);
          }
         
          if($type=="get_chat_list"){
            //查询历史聊天记录
           
            $chatlist=M('service_log')->where('from_uid='.$user_id.' or to_uid='.$user_id)->limit($start,$pcount)->order('id asc')->select();
            rsort($chatlist);
            echo json_encode($chatlist);
          }
                
          exit;
       }

       $this->assign('user',$user);
       $this->assign('leve_mesg',$leve_mesg);
       $this->assign('page',$page);
       $this->assign('kefuinfo',json_encode($kefu_info));
       echo $this->fetch();
    }
    public function count_message(){
      $user_id=I('post.user_id');
      $count=M('service_log')->where(['to_uid'=>$user_id,'status'=>'offline'])->count();
      echo $count;
    }
    public function send_message()
    {
       $msg_type=I('post.msg_type');
       $to_uid = I('post.to');
       $from_uid = I('post.from');
       if($to_uid==''&&$_COOKIE['to_uid']){
          $to_uid=$_COOKIE['to_uid'];
          $iscookie=$to_uid;
       }
       $content=I('post.content');
       $time=I('post.time');
       $post_data['nickname']=I('post.nickname');
       $post_data['head_pic']=I('post.head_pic');
       // 推送的url地址，使用自己的服务器地址
       $push_api_url = "http://dlshop.dliansoft.com:2121/";
       $post_data['type']="publish";
       $post_data['content']=$content;
       $post_data['to']=$to_uid;
       $post_data['from']=$from_uid;
       $post_data['time']=$time;
       $post_data['msg_type']=$msg_type;

       $ch = curl_init ();
       curl_setopt ( $ch, CURLOPT_URL, $push_api_url );
       curl_setopt ( $ch, CURLOPT_POST, 1 );
       curl_setopt ( $ch, CURLOPT_HEADER, 0 );
       curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 );
       curl_setopt ( $ch, CURLOPT_POSTFIELDS, $post_data );
       curl_setopt ($ch, CURLOPT_HTTPHEADER, array("Expect:"));
       $return= curl_exec( $ch );
       curl_close ( $ch );
       $result=json_decode($return,true);
       
        $data['from_uid']= $from_uid;
        $data['to_uid']= $result['to_uid'];
        $data['message']= $content;
        $data['time']=strtotime($time);
        $data['type']=$msg_type;
        $data['status']=$result['status'];
        $result['message_id']=M('service_log')->add($data);
        //setcookie('to_uid',$data['to_uid'],time()+3600*24*30);
       
       $result['cookie']=$iscookie;

       echo json_encode( $result);
       
    }
    public function get_kefu_re_message()
    {
      $re_list=I('post.re_list');
      $msg_type=I('post.msg_type');
      $from_uid=I('post.from_uid');
      if($msg_type==2){
        $field='t0.from_uid,t0.message,t0.time,t1.nickname,t1.head_pic';
        $where='(t0.to_uid in ("'.$re_list.'") and t0.status="offline") or t0.status="no_kefu_noline"';
        
        
        $wait_re=M('service_log t0')->field($field)->join('tp_users t1','t0.from_uid=t1.user_id','left')->where($where)->select();
 
        foreach ($wait_re as $key => $value) {
          $value['nickname']=base64_decode($value['nickname']);
          $value['content']=$value['message'];
          $value['from']=$value['from_uid'];
          $value['time']=date('Y-m-d H:i:s',$value['time']);
          $wait_re[$key]=$value;
        }
      }
      if($msg_type==1){
        $where='t0.to_uid in ("'.$re_list.'") and t0.status="offline"';
        $field='t0.to_uid,t0.message,t0.time,t1.kf_name,t1.kf_headpic';
        $wait_re=M('service_log t0')->field($field)->join('tp_service t1','t0.from_uid=t1.kf_no','left')->where($where)->select();
      }

      M('service_log t0')->where($where)->save(array('status'=>'ok','to_uid'=>$from_uid));

      echo json_encode($wait_re);
    }

    public function auto_replay(){

    }
}